package com.lkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.dto.SkuDto;
import com.lkd.entity.Sku;
import com.lkd.entity.SkuClass;
import com.lkd.mapper.SkuClassMapper;
import com.lkd.mapper.SkuMapper;
import com.lkd.service.ISkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.vo.PageBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author zzy
 * @since 2025-10-10
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements ISkuService {

    @Autowired
    private SkuClassMapper skuClassMapper;

    @Override
    public PageBean<Sku> search(SkuDto skuDto) {
        //1.设置分页参数
        Page<Sku> page = new Page<Sku>(skuDto.getPageIndex(), skuDto.getPageSize());
        //2.设置业务参数
        LambdaQueryWrapper<Sku> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(skuDto.getSkuName()), Sku::getSkuName, skuDto.getSkuName());

        //3.1执行分页查询
        page = this.page(page, wrapper);

        //3.2获取到page中当前页集合 遍历集合获取到其中每个商品 然后根据商品的classid去查询分类信息 再将结果封装到sku中的skuClass属性上
        page.getRecords().forEach(sku -> {
            SkuClass skuClass = skuClassMapper.selectById(sku.getClassId());
            sku.setSkuClass(skuClass);
        });

        //4.构建返回结果
        return new PageBean<>(page);
    }
}
